import {Rule} from "antd/lib/form";
import {RcFile} from "antd/es/upload";
import {message} from "antd";


export const MODAL_WIDTH: number = 600;
export const SELECT_WIDTH: number = 520;


/**
 * 医院综合（科室管理、添加科室）表单验证规则
 */

// 输入框验证规则
export const departmentNameRules: Rule[] = [
    {
        required: true,
        message: '科室名称不能为空！',
    },
    {
        required: true,
        pattern: /^[\u4e00-\u9fa5]+$/, // 正则表达式，匹配中文字符
        message: '科室名称必须为中文！',
    },
];

// 下拉列表验证规则
export const mainDoctorRules: Rule[] = [
    {
        required: true,
        message: '请选择科室主任！',
    },
];

// ==============================以下是全账号管理表单验证

export const accountRules: Rule[] = [
    {required: true, message: '请输入账号名/手机号/邮箱'},
    {type: 'string', min: 5, max: 20, message: '账号长度需在 5 到 20 个字符之间'},
];

export const purviewRules: Rule[] = [
    {required: true, message: '请选择账号权限'},
];
export const departmentRules: Rule[] = [
    {required: true, message: '请选择账号科室'},
];

export const temporaryRules: Rule[] = [
    {required: true, message: '请输入密码'},
    {min: 6, message: '密码至少为 6 个字符'},
];

export const passwordRules: Rule[] = [
    {required: true, message: '请确认密码'},
    ({getFieldValue}) => ({
        validator(_, value) {
            if (!value || getFieldValue('temporary') === value) {
                return Promise.resolve();
            }
            return Promise.reject(new Error('两次输入的密码不匹配'));
        },
    }),
];

export const nameRules: Rule[] = [
    {required: true, message: '请输入该医务人员姓名'},
];

export const sexRules: Rule[] = [
    {required: true, message: '请选择性别'},
];

export const idcardRules: Rule[] = [
    {required: true, message: '请输入该医务人员身份证号'},
    // { pattern: /^[1-9]\d{5}(18|19|20)\d{2}\d{2}\d{2}\d{3}(\d|X)$/, message: '请输入有效的身份证号' }
];

export const phoneRules: Rule[] = [
    {required: true, message: '请输入手机号'},
    {pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号'}
];

export const headRules: Rule[] = [
    {required: true, message: '请上传照片'},
];

// ==============================以上是全账号管理表单验证

// 图片上传限制
export const beforeUpload = (file: RcFile) => {
    const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
    if (!isJpgOrPng) {
        message.error('你只能上传image/jpeg格式的图片');
    }
    const isLt2M = file.size / 1024 / 1024 < 2;
    if (!isLt2M) {
        message.error('图片大小必须小于2MB');
    }
    return isJpgOrPng && isLt2M;
};

// Base64编码获取
export const getBase64 = (file: RcFile): Promise<string> =>
    new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => resolve(reader.result as string);
        reader.onerror = error => reject(error);
    });
